package com.slz3.business.module.wmsManagement.wmsStockPut.service;

import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.wmsManagement.wmsStockDetail.domain.WmsStockDetail;
import com.slz3.business.module.wmsManagement.wmsStockDetail.domain.WmsStockDetailFactory;
import com.slz3.business.module.wmsManagement.wmsStockPut.domain.WmsStockPut;
import com.slz3.business.module.wmsManagement.wmsStockPut.domain.WmsStockPutFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class WmsStockPutTransactionalServiceImpl implements WmsStockPutTransactionalService {

    @Override
    public WmsStockPut save(WmsStockPut wmsStockPut) {
        WmsStockPut wmsStockPutNew = Repositorys.wmsStockPut.save(WmsStockPutFactory.initBaseFields(wmsStockPut));
        if(VE.isNotEmpty(wmsStockPut.getStockDetailList())){
            List<WmsStockDetail>  stockDetailList = wmsStockPut.getStockDetailList();
            stockDetailList.stream().forEach(stockDetail ->{
                stockDetail.setUpper(wmsStockPutNew.getUuid());
                WmsStockDetailFactory.initBaseFields(stockDetail);
            });
            Repositorys.wmsStockDetail.saveAll(stockDetailList);
        }
        return wmsStockPutNew;
    }

    @Override
    public WmsStockPut modify(WmsStockPut wmsStockPutNew) {
        Repositorys.wmsStockPut.test();
        Repositorys.wmsStockPut.save(WmsStockPutFactory.initBaseFields());
        System.err.println(1 / 0);
        wmsStockPutNew.setLastModifyTime(LocalDateTime.now());
        return Repositorys.wmsStockPut.saveAndFlush(wmsStockPutNew);
    }

    @Override
    public List<WmsStockPut> batchModify(List<WmsStockPut> wmsStockPutList) {
        return Repositorys.wmsStockPut.saveAll(wmsStockPutList);
    }
}
